<?php
/**
 * Menu item active record
 *
 * @author Lukas Normantas
 */
class Content_Model_Menu extends Doctrine_Record
{
    protected $_menuForm = null;

    public function setTableDefinition()
    {
        $this->setTableName('catalog_menus');
        $this->hasColumn('album_id', 'integer');
        $this->hasColumn('title', 'string', 255);
        $this->hasColumn('body', 'clob');
        $this->hasColumn('active', 'boolean', 25);
    }

    public function setUp()
    {
        $this->actAs('NestedSet');
        $this->actAs('Timestampable');

        $this->hasOne('Content_Model_Album as Album', array(
            'local' => 'album_id',
            'foreign' => 'id'
        ));

    }

    static protected function _ensureTreeHasRoot(Doctrine_Tree $tree)
    {
        if ( ! $tree->fetchTree() ){
            $menu = new Content_Model_Menu();
            $menu->title = 'Menu';
            $menu->active = 1;
            $menu->save();
            $tree->createRoot($menu);
        }
    }

    public function getAdminSubmenuList()
    {
        $children = $this->getNode()->getChildren();
        return $children;
    }

    public function getActiveSubmenuList()
    {
        $q = Doctrine_Query::create()
           ->select( 'c.*')
           ->from('Content_Model_Menu m')
           ->where('active = ?', 1);
        $this->getTable()->getTree()->setBaseQuery($q);
        $children = $this->getNode()->getChildren();
        if ( ! $children) return false;
        return $children->toArray();
    }

    static public function getAdminMenuTree()
    {
        $treeObject = Doctrine::getTable('Content_Model_Menu')->getTree();
        $q = Doctrine_Query::create()
            ->select('m.*')
            ->from('Content_Model_Menu m');
        $treeObject->setBaseQuery($q);
        foreach ($treeObject->fetchRoots() as $rootCategory) {}
        return $rootCategory;
    }

    static public function getSiteMenuTree()
    {
        $treeObject = Doctrine::getTable('Content_Model_Menu')->getTree();
        $q = Doctrine_Query::create()
            ->select('m.*')
            ->from('Content_Model_Menu m')
            ->where('active = ?', 1);
        $treeObject->setBaseQuery($q);
        foreach ($treeObject->fetchRoots() as $rootCategory) {}
        return $rootCategory;
    }

    static public function getCategoryMenuTree()
    {
        $treeObject = Doctrine::getTable('Content_Model_Menu')->getTree();
        self::_ensureTreeHasRoot($treeObject);
        $q = Doctrine_Query::create()
            ->select('c.*')
            ->from('Content_Model_Menu m');
        $treeObject->setBaseQuery($q);
        foreach ($treeObject->fetchRoots() as $rootCategory) {}
        return $rootCategory;
    }

    static public function getMenuSelectData()
    {
        $treeObject = Doctrine::getTable('Content_Model_Menu')->getTree();
        self::_ensureTreeHasRoot($treeObject);
        $q = Doctrine_Query::create()->from('Content_Model_Menu m');
        $treeObject->setBaseQuery($q);
        $data = array();
        foreach ($treeObject->fetchTree() as $menu){
            $data[$menu->id] = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;',$menu->level) . $menu->title;
        }
        return $data;
    }

    static public function addMenusToNavigation($navigation, $menu_id)
    {
        $menu = Doctrine::getTable('Content_Model_menu')->find($menu_id);
        $ancestors = $menu->getNode()->getAncestors();
        $oldPage = $navigation->findOneBy('label', 'Menu');
        $tmpPages = $oldPage->getPages();
        if ($ancestors) foreach ($ancestors as $ancestor){
            $options = array(
                'label'      => $ancestor->title,
                'module'     => 'content',
                'controller' => 'menu',
                'action'     => 'admin',
                'params'     =>array('menu_id'=> $ancestor->id)
            );
            $oldPage->setOptions($options);
            $newPage = new Zend_Navigation_Page_Mvc();
            $oldPage->addPage($newPage);
            $oldPage = $newPage;
        }
        $options = array(
            'label'      => $menu->title,
            'module'     => 'content',
            'controller' => 'menu',
            'action'     => 'admin',
            'params'     =>array('menu_id'=> $menu->id)
        );
        $oldPage->setOptions($options);
        $oldPage->setPages($tmpPages);
    }

    public function getForm(){
        if ($this->_menuForm == null){
            $this->_menuForm = new Content_Form_MenuEditing();
            $this->_menuForm->setDefaults($this->toArray());
        }
        return $this->_menuForm;
    }

    public function add($data, $parent_id)
    {
        $form = $this->getForm();
        if ($form->isValid( $data )){
            $parentMenu = Doctrine::getTable('Content_Model_Menu')->find($parent_id);
            $newMenu = new Content_Model_Menu();
            $newMenu->fromArray($form->getValues());
            $newMenu->getNode()->insertAsLastChildOf($parentMenu);
            return true;
        }
        return false;
    }

    public function edit($data)
    {
        $form = $this->getForm();
        if ($form->isValid( $data )){
            $this->fromArray($form->getValues());
            $this->save();
            return true;
        }
        return false;
    }

    protected $_moveForm = null;

    public function getMoveForm(){
        if ($this->_moveForm == null){
            $this->_moveForm = new Content_Form_MenuSelect();
            $this->_moveForm->setDefault('selected_menu_id', $this->getNode()->getParent()->id);
        }
        return $this->_moveForm;
    }


    public function move($data)
    {
        $form = $this->getMoveForm();
        if ($form->isValid( $data )){
            $this->fromArray($form->getValues());
            $parent_id = $form->getValue('selected_menu_id');
            $newParent = Doctrine::getTable('Content_Model_Menu')->find($parent_id);
            
            if ( $newParent->getNode()->isDescendantOf($this) ) {
                return false;
            }
            if ( $newParent->getNode()->isEqualTo($this) ) {
                return false;
            }

            $this->getNode()->moveAsLastChildOf($newParent);    
            
            return true;
        }
        return false;
    }


}